﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="../css/themes/default/jquery.mobile-1.4.2.min.css" rel="stylesheet" type="text/css" />
    <script src="../js/jquery.js" type="text/javascript"></script>
    <script src="../js/jquery.mobile-1.4.2.js" type="text/javascript"></script>
</head>
<body>
    <div data-role="header" data-position="fixed" data-theme="a">
        <a href="../" data-rel="back" class="ui-btn ui-btn-left ui-alt-icon ui-nodisc-icon ui-corner-all ui-btn-icon-notext ui-icon-carat-l">
            Back</a>
        <h1>
            设置</h1>
    </div>
    <!--提示-->
    <div id="popupBasic" data-role="popup">
        <p id="pInfo">
        </p>
    </div>
    <a data-role="button" id="A1" data-theme="b">创建数据库、表</a>
    <script type="text/javascript">
        $("#A1").click(function () {
            //alert("准备创建数据库SugarDB,开始生成。。。。。");
            $("#popupBasic").popup("open");
            $("#pInfo").text("准备创建数据库SugarDB,开始生成。。。。。");
            // setTimeout("InitializeDatabase()", 1000);
            InitializeDatabase();
        });
        function InitializeDatabase() {
            var db = window.openDatabase("SugarDB", "1.0", "SugarDB", 5000000);
            db.transaction(DoCreateDataTable, errorCB);
        }
        function DoCreateDataTable(tx) {
            //UserAccount
            var CheckDataUserAccount = "SELECT count(*) AS cnt FROM sqlite_master WHERE type='table' AND name='UserAccount';";
            tx.executeSql(CheckDataUserAccount, [], UserAccountSuccess, errorCB);
            //DATABILL
            var CheckDataBill = "SELECT count(*) AS cnt FROM sqlite_master WHERE type='table' AND name='DATABILL';";
            tx.executeSql(CheckDataBill, [], BillSuccess, errorCB);
            closeB();
        }
        function UserAccountSuccess(tx, results) {
            var tablecount = results.rows.item(0).cnt;
            //判断是否存在表
            if (tablecount <= 0) {
                //创建数据登录用户信息表
                var cbUsersql = "create table if not exists UserAccount (id integer not null primary key,userid ,username text,password text)";
                tx.executeSql(cbUsersql);
            }
        }
        //
        function BillSuccess(tx, results) {
            var tablecount = results.rows.item(0).cnt;
            //判断是否存在表
            if (tablecount <= 0) {
                //创建数据登录用户信息表
                var cbBillsql = "create table if not exists DATABILL (id integer not null primary key ,name text,price text,details text,createtime text,userid text,username text)";
                tx.executeSql(cbBillsql);
            }
        }
        function closeB() {
            $("#popupBasic").popup("close");
        }
        function errorCB(err) {
            console.log("错误处理的SQL：" + err.code + "错误信息：" + err.message);
            alert("错误信息：" + err.message);
        }

        // 事务执行成功后调用的回调函数
        function successCB() {
            $("#pInfo").text("创建成功");
        }


    </script>
    <a data-role="button" id="A2" data-theme="b">清空数据表</a>
    <script type="text/javascript">
        $("#A2").click(function () {
            $("#popupBasic").popup("open");
            //  alert("准备清空数据库001,开始清空数据。。。。。");
            $("#pInfo").text("准备清空数据库准备创建数据库SugarDB,开始清空数据表。。。。。");
            var db = window.openDatabase("SugarDB", "1.0", "SugarDB", 5000000);
            db.transaction(queryDB, errorCB, successCB);
        });
        // 查询数据库
        function queryDB(tx) {
            //删除表 
            var dropDataTable = "DROP TABLE IF EXISTS  DATABILL";
            tx.executeSql(dropDataTable);
        }
        function errorCB(err) {
            console.log("错误信息：" + err.message);
            alert("错误信息：" + err.message);

        }
        // 事务执行成功后调用的回调函数
        function successCB() {
            //    alert("清空数据成功！");
            $("#pInfo").text("清空数据表成功");
            setTimeout("closeB()", 1000);

        }
        function closeB() {
            $("#popupBasic").popup("close");
        }
    </script>
    <a data-role="button" id="A3" data-theme="b">清空所有得数据</a>
    <script type="text/javascript">
        $("#A3").click(function () {
            $("#popupBasic").popup("open");
            //  alert("准备清空数据库001,开始清空数据。。。。。");
            $("#pInfo").text("准备清空SugarDB,开始清空数据。。。。。");
            var db = window.openDatabase("SugarDB", "1.0", "SugarDB", 5000000);
            db.transaction(queryDB, errorCB, successCB);
        });
        //  
        function queryDB(tx) {
            //删除数据 
            var dropDataTable = "delete from DATABILL";
            tx.executeSql(dropDataTable);
        }
        function errorCB(err) {
            console.log("错误信息：" + err.message);
            alert("错误信息：" + err.message);

        }
        // 事务执行成功后调用的回调函数
        function successCB() {
            //    alert("清空数据成功！");
            $("#pInfo").text("清空数据成功");
            setTimeout("closeB()", 1000);

        }
        function closeB() {
            $("#popupBasic").popup("close");
        }
    </script>
    <div>
        <label for="name">
            服务器地址:</label>
        <input type="text" name="serviceurl" id="serviceurl" placeholder="192.168.1.19" value="192.168.1.19"
            data-clear-btn="true" />
        <a data-role="button" id="A4" data-theme="b">同步数据</a>
        <script type="text/javascript">
            var serviceurl;
            $(function () {
                $("#A4").click(function () {
                    serviceurl = document.getElementById("serviceurl").value;
                    if (serviceurl == null || serviceurl == "") {
                        alert("请输入服务器地址！");
                        return;
                    }
                    synchronousdata(serviceurl);
                });

                //同步数据
                function synchronousdata(serviceurl) {
                    $("#popupBasic").popup("open");
                    $("#pInfo").text("正在同步SugarDB数据。。。。。");
                    //  setTimeout("closeB()", 1000);
                    var db = window.openDatabase("SugarDB", "1.0", "SugarDB", 5000000);
                    db.transaction(querySB, errorSB, successSB);
                }
                // 查询数据 
                function querySB(tx) {
                    var selectsql = "select * from DATABILL";
                    tx.executeSql(selectsql, [], querySuccess, errorSB);
                }
                function querySuccess(tx, results) {
                    if (results.rows.length != 0) {
                        var data = "[";
                        for (var i = 0; i < results.rows.length; i++) {
                            var id = results.rows.item(i).id;
                            var name = results.rows.item(i).name;
                            var img = results.rows.item(i).img;
                            var createtime = results.rows.item(i).createtime;
                            var price = results.rows.item(i).price;
                            var details = results.rows.item(i).details;
                            data += "{id:'" + id + "',name:'" + name + "',createtime:'" + createtime + "',price:'" + price + "',details:'" + details + "'}";
                            if (i < results.rows.length - 1) {
                                data += ",";
                            }
                        }
                        data += "]";
                        //        var jsonBook = JSON.stringify(book);
                        //                        var objectBook = JSON.parse(jsonBook);
                        var data2 = JSON.stringify(results.rows.item);
                        alert(data2);
                        docheckajax(data);
                    }
                    else {
                        $("#pInfo").text("没有数据不需要同步！");
                    }
                }
                function docheckajax(results) {
                    //开始同步数据
                    $.ajax({
                        type: "post",
                        dataType: "text",
                        url: "ashx/synchronousdata.ashx?action=synchronousdata",
                        data: { url: serviceurl, data: results },
                        success: function (status) {
                            if (status == "0") {
                                alert("成功！");
                            }
                            else {
                                alert('同步失败!');
                            }
                        },
                        error: function (err) {
                            alert(err.responseTest); ;
                        },
                        complete: function () {

                        }
                    });
                }

                function closeB() {
                    $("#popupBasic").popup("close");
                }
                function errorSB(err) {
                    console.log("错误处理的SQL：" + err.code + "错误信息：" + err.message);
                    alert("错误信息：" + err.message);
                    return false;
                }
                function successSB() {

                }
            })
        </script>
    </div>
</body>
</html>
